﻿<!DOCTYPE html>
<html>
<head>
	<title>达夫设备测试</title>
</head>
<body>
	<script type="text/javascript">
		var chars = '0123456789abcdef';

		function getRandomString() {
			var len = Math.ceil(Math.random() * 7) + 3; // 4-10
			var result = "";
			while (len--) {
				result += chars.charAt(Math.floor(Math.random() * chars.length));
			}
			return result;
		}

		var size = 10000;
		var mult = 100;

		var ary = [];
		var lsize = size;
		while (lsize--) { ary.push('' + getRandomString() + ''); }

		function process(item) {
			var isInt = /(^[0-9]$)|(^[1-9][0-9]+$)/;
			if (isInt.test(item)) { item += 1; }
		}

		function for_normal_fn() {
			for (var i = 0, len = ary.length; i < len; i++) { process(ary[i]); }
		}

		function for_normal() {
			var isInt = /(^[0-9]$)|(^[1-9][0-9]+$)/;
			for (var i = 0, len = ary.length; i < len; i++) {
				var item = ary[i];
				if (isInt.test(item)) { item += 1; }
			}
		}

		function while_normal() {
			var isInt = /(^[0-9]$)|(^[1-9][0-9]+$)/,
					i = 0,
					l = ary.length;
			while (i < l) {
				var item = ary[i++];
				if (isInt.test(item)) { item += 1; }
			}
		}

		function duff_normal() {
			var i = 0,
					l = ary.length,
					n = l % 8;
			while (n--) {
				process(ary[i++]);
			}

			n = parseInt(l / 8);
			while (n--) {
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
				process(ary[i++]);
			}
		}

		function duff_fast() {
			var isInt = /(^[0-9]$)|(^[1-9][0-9]+$)/,
					item, i = 0,
					l = ary.length,
					n = l % 8;
			while (n--) {
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
			}

			n = parseInt(l / 8);
			while (n--) {
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
				item = ary[i++];
				if (isInt.test(item)) { item += 1; }
			}
		}

		console.log('数组长度：' + ary.length);
		console.log('Profiling will begin in 2 seconds...');
		setTimeout(function () {
			console.log('普通for循环测试：')
			var currTime = new Date();
			console.profile();
			for (var i = 0; i < mult; i++) {
				for_normal();
			}
			console.profileEnd();
			console.log('用时：' + (new Date() - currTime) + 'ms');

			//			console.log('for循环内调用外部函数测试：')
			//			currTime = new Date();
			//			console.profile();
			//			for (var i = 0; i < mult; i++) {
			//				for_normal_fn();
			//			}
			//			console.profileEnd();
			//			console.log('用时：' + (new Date() - currTime) + 'ms');

			console.log('普通while循环测试：')
			var currTime = new Date();
			console.profile();
			for (var i = 0; i < mult; i++) {
				while_normal();
			}
			console.profileEnd();
			console.log('用时：' + (new Date() - currTime) + 'ms');

			//			console.log('达夫设备循环测试：')
			//			currTime = new Date();
			//			console.profile();
			//			for (var i = 0; i < mult; i++) {
			//				duff_fast();
			//			}
			//			console.profileEnd();
			//			console.log('用时：' + (new Date() - currTime) + 'ms');

			console.log('达夫设备调用外部函数循环测试：')
			currTime = new Date();
			console.profile();
			for (var i = 0; i < mult; i++) {
				duff_normal();
			}
			console.profileEnd();
			console.log('用时：' + (new Date() - currTime) + 'ms');

		}, 2000);

	</script>
</body>
</html>
